{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc1b4100",
   "metadata": {},
   "source": [
    "# Plot Margins\n",
    "\n",
    "`plot_margin` parameter in `theme()` allows to change the margin areas of a plot using the `margin()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11298efb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"kYdVw3\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.1.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"kYdVw3\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"kYdVw3\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "13ff2ed9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.5.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.1.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8500fa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "val rand = java.util.Random()\n",
    "\n",
    "val data = mapOf(\"x\" to List(100) { rand.nextInt(10) })\n",
    "\n",
    "val p = letsPlot(data) { x = \"x\" } + geomBar() + ggtitle(\"Bar Сhart\") +\n",
    "    theme(plotBackground = elementRect(size = 6))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57bc28a9",
   "metadata": {},
   "source": [
    "#### Plot without Margins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1fc6984a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"0VsozL\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Bar Сhart\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[14.0,8.0,11.0,10.0,9.0,10.0,8.0,11.0,6.0,13.0],\n",
       "\"x\":[7.0,6.0,3.0,1.0,4.0,8.0,2.0,5.0,9.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"plot_background\":{\n",
       "\"size\":6.0,\n",
       "\"blank\":false\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"0VsozL\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bb41b1d",
   "metadata": {},
   "source": [
    "#### Plot with Margins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d19ad8bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"V6U4mn\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"ggtitle\":{\n",
       "\"text\":\"Bar Сhart\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "\"..count..\":[14.0,8.0,11.0,10.0,9.0,10.0,8.0,11.0,6.0,13.0],\n",
       "\"x\":[7.0,6.0,3.0,1.0,4.0,8.0,2.0,5.0,9.0,0.0]\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"plot_background\":{\n",
       "\"size\":6.0,\n",
       "\"blank\":false\n",
       "},\n",
       "\"plot_margin\":{\n",
       "\"t\":20.0,\n",
       "\"r\":10.0,\n",
       "\"b\":10.0,\n",
       "\"l\":10.0\n",
       "}\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"V6U4mn\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p + theme(plotMargin = margin(t=20,  // Top\n",
    "                              r=10,  // Right\n",
    "                              b=10,  // Bottom\n",
    "                              l=10)) // Left      "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
